/*
Copyright (C) Ariff Ehsan, Abdul Hadi Fikri

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*  Author : Ariff Ehsan
 *  Date   : July 11, 2009
 */


#ifndef __CXBEE_H__
#define __CXBEE_H__


/**
 *  load default setting
 */
#ifndef CXBEE_ADDRESS
#define CXBEE_ADDRESS
#warning cxbee address in not defined. set to 0.
#endif

#ifndef CXBEE_DEF_CHANNEL
#define CXBEE_DEF_CHANNEL 12
#endif

#ifndef CXBEE_DEF_TXPOWER
#define CXBEE_DEF_TXPOWER 4
#endif

#ifndef CXBEE_DEF_MAC
#define CXBEE_DEF_MAC 2
#endif

#define CXBEE_DATA_LENGTH 100

#ifdef CXBEE_BAUD
#define SERIAL0_BAUD CXBEE_BAUD
#endif


#include <cxbee/cxbee_type.h>
#include <serial0/serial0.h>


/** ---------------------------------------------------------------------------
 *                           CXBEE LIBRARY API
 *  ---------------------------------------------------------------------------
 */

/**
 *  Start using XBEE library
 *
 *	@return - error type
 */
error_t CXBEE_start(void);


/**
 *  Send message
 *
 *  @dest - 16-bit destinationn address
 *  @data - pointer to data packet
 *  @len  - length of payload in data packet
 *
 *  @return - error type:
 *            1. EBUSY  - module is in busy state.
 *            2. EINVAL - invalid destination address
 *            3. ESIZE  - invalid data size
 */
error_t CXBEE_send(uint16_t dest, message_t *data, uint8_t len);


/**
 *  Register/Wire xbee event functions
 *
 *  @startdone - pointer to start done event function
 *  @senddone  - pointer to send done event function
 *  @receive   - pointer to receive event function
 */
void CXBEE_reg(void(*startdone)(error_t),void(*senddone)(error_t),
               message_t *(*receive)(message_t*,void*,uint8_t));


/** ---------------------------------------------------------------------------
 *                           CXBEE PACKET API
 *  ---------------------------------------------------------------------------
 */

/**
 *  Get payload pointer from the given data packet
 *
 *  @data - pointer to the packet
 *
 *  @return - pointer to payload of the packet
 */
void *CXBEE_getPayload(message_t *data);


/**
 *  Enable or disable acknowledgement for the given packet.
 *
 *  @data - pointer to the packet
 *  @option - FALSE (disable ack) or TRUE (enable ack)
 */
void CXBEE_setAck(message_t *data, BOOL option);


/**
 *  Set transmit power level for the given data packet.
 *
 *  @data  - pointer to the packet
 *  @level - level of transmit power:
 *
 *           0 = -10 dBm
 *           1 =  -6 dBm
 *           2 =  -4 dBm
 *           3 =  -2 dBm
 *           4 =   0 dBm
 */
void CXBEE_setTxPower(message_t *data, uint8_t level);


/** 
 *  Get received signal strength indicator (RSSI) value
 *
 *  @msg - pointer to the given packet
 *
 *  @return - the RSSI value is a signed value (negative dBm)
 */
int8_t CXBEE_getRssi(message_t *msg);


/**
 *  Get source address of the received msg
 *
 *  @msg - pointer to the given packet
 *
 *	@return - the 16-bit source address
 */
uint16_t CXBEE_getSource(message_t *msg);


#endif // __CXBEE_H__